<?php

namespace app\admin\model\member;

use think\Db;
use think\Model;
use app\admin\model\vactivities\Activity;
use app\admin\model\superdata\Activitycategory as cat;


class Account extends Model
{

    //添加活动参与人类型  1 前端购买 2 后台添加
    const ADD_ACTIVITY_TYPE_1 = 1;
    const ADD_ACTIVITY_TYPE_2 = 2;


    // 表名
    protected $table = 'account';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'prevtime_text',
        'logintime_text',
        'operationtime_text',
        'source_text',
        'TLS_text',
        'isOld_text'
    ];


    
    public function getSourceList()
    {
        return ['0' => __('Source 0'), '1' => __('Source 1'), '2' => __('Source 2'), '3' => __('Source 3'), '4' => __('Source 4'), '5' => __('Source 5')];
    }

    public function getTlsList()
    {
        return ['1' => __('Tls 1'), '0' => __('Tls 0')];
    }

    public function getIsoldList()
    {
        return ['0' => __('Isold 0'), '1' => __('Isold 1')];
    }


    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOperationtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['operationtime']) ? $data['operationtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSourceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['source']) ? $data['source'] : '');
        $list = $this->getSourceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTlsTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['TLS']) ? $data['TLS'] : '');
        $list = $this->getTlsList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsoldTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['isOld']) ? $data['isOld'] : '');
        $list = $this->getIsoldList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPrevtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setOperationtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    /*
     * 通过活动id与参与人id查找是否有此用户
     */
    public function getrelIdByaIdAndactId($aid,$activity_id){
        $res = Db::name('activity_participants')->where('account_id',$aid)->where('activity_id',$activity_id)->field('id')->find();
        if($res){
            return true;
        }else{
            return false;
        }

    }

    /*
     * 通过id查找账号
     */
    public static  function getUsrNameById($id){
        $res = Db::name('account')->where('id',$id)->field('username')->find();
        if($res){
            $name = $res['username'];
        }else{
            $name = '';
        }
        return $name;
    }
    /*
   * 通过id查找用户所有信息
   */
    public static  function getUsrInfoById($id)
    {
        if(empty($id))
            return false;
        $res = Db::name('admin')->where('id', $id)->field('username,nickname,mobile,avatar,introduction')->find();
        if ($res) {
            $r = Db::name('admin_basic_zoom')->where('admin_id', $id)->field('avatar,intro')->find();
            $res['avatar'] = isset($r) && !empty($r['avatar']) ? $r['avatar']:'';
            $res['introduction'] = isset($r) && !empty($r['intro']) ? $r['intro']:'';
            return $res;
        } else {
            return false;
        }
    }
    /*
      * 通过id查找发布的所有活动
      */
    public static function getActInfoById($id)
    {
        if(empty($id))
            return false;
        $map['status'] = Activity::ACTIVITY_STATUS_1;
        $res = Db::name('activity')->where('admin_id', $id)->where($map)->field('id,name,thumb,summary,detail,startTime')->find();
        if ($res) {
            return $res;
        } else {
            return false;
        }
    }
    /*
     * 统计发布的所有活动所属分类
     */

    public function getCatByUid($uid){

        if(empty($uid))
            return false;
        $map['status'] = Activity::ACTIVITY_STATUS_1;
        $res = Db::name('activity')->where('admin_id', $uid)->where($map)->group('category')->field('category')->select();

        if ($res) {
            foreach($res as $k=>$v){
                $list[] = self::getCatNameById($v['category']);
            }
            $list = array_filter($list);
            $list = array_values($list);
            unset($map);
            //print_r($list);
            return $list;
        } else {
            return false;
        }
    }
    /*
     * 统计发布的所有活动所属标签
     */
    public function getTagsByUid($uid){
        if(empty($uid))
            return false;
        $map['status'] = Activity::ACTIVITY_STATUS_1;
        $res = Db::name('activity')->where('admin_id', $uid)->where($map)->group('tags')->field('tags')->select();
        if ($res) {
            foreach($res as $k=>$v){
                $l[] = self::getTagNameById($v['tags']);
            }
            $l = array_filter($l);
            //print_r($l);die;
            $l = array_values($l);
            unset($map);
            return $l;
        } else {
            return false;
        }
    }

    /*
     * 通过分类id查找分类名称
     */
    static private function getCatNameById($id){
        if(empty($id))
            return false;
        $map['status'] = cat::ACTIVITY_STATUS_1;
        $res = Db::name('activity_category')->where('id', $id)->where($map)->field('name')->find();
        if($res)
            return $res['name'];
        else
            return '';
    }

    /*
      * 通过标签id查找标签名称
    */
    static private function getTagNameById($id){
        if(empty($id))
            return false;
        $map['status'] = cat::ACTIVITY_STATUS_1;
        $res = Db::name('activity_tags')->where('id', $id)->where($map)->field('name')->find();
        if($res)
            return $res['name'];
        else
            return '';
    }
}
